<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import request from '@/utils/request' // 导入封装好的request实例

// 定义响应式数据
const carouselMovie = ref([])
const hotMovieList = ref([])
const topRateMovie = ref([])
const classicalMovie = ref([])

const router = useRouter()
// 跳转到电影详情页，
const toMovieDetail = (movieId) => {
  // 使用router实例跳转到电影详情页,在URL中显示为：/movie_detail?gid=123
  router.push({ path: '/userindex/movieDetail', query: { gid: movieId } })
  // router.push(`/movie_detail?gid=${movieId}`)
}

// 获取轮播图数据
const getCarouselMovie = async () => {
  // 使用request实例发送请求
  const data = await request.get("/goods/carouselMovie")
  carouselMovie.value = data

}

// 获取按积分排序
const getHotMovieList = async () => {
  const data = await request.get("/goods/hotMovieList")
  hotMovieList.value = data
}

// 获取经典书籍
const getTopRateMovie = async () => {
  const data = await request.get("/goods/topRateMovie")
  topRateMovie.value = data
}

// 获取价格高低商品
const getClassicalMovie = async () => {
  const data = await request.get("/goods/classicalMovie")
  classicalMovie.value = data
}

// 生命周期钩子
onMounted(() => {
  
  getCarouselMovie()
  getHotMovieList()
  getTopRateMovie()
  getClassicalMovie()
})
</script>

<template>
  <div class="index-container">
    <!-- 轮播图使用elementplus中的el-carousel走马灯示例-->
    <el-card class="carousel-card">
      <el-carousel :interval="4000" type="card" height="240px" indicator-position="outside">
        <el-carousel-item v-for="item in carouselMovie" :key="item.gid">
          <img @click="toMovieDetail(item.gid)" v-lazy="item.img" :src="item.img" :alt="item.gname"
            class="carousel-image" />
          <span class="carousel-title">{{ item.gname }}</span>
        </el-carousel-item>
      </el-carousel>
    </el-card>

    <div class="content-container">
      <div class="main-content">
        <el-card class="movie-section">
          <h2 class="section-title hot">正在热卖</h2>
          <div class="movie-grid">
            <div v-for="item in hotMovieList" :key="item.gid" class="movie-card" @click="toMovieDetail(item.gid)"
              v-lazy="item.img">
              <img :src="item.img" :alt="item.gname" class="movie-poster">
              <p class="movie-title">{{ item.gname }}</p>
            </div>
          </div>
        </el-card>

        <el-card class="movie-section">
          <h2 class="section-title classical">优惠书籍</h2>
          <div class="movie-grid">
            <div v-for="item in classicalMovie" :key="item.gid" class="movie-card" @click="toMovieDetail(item.gid)"
              v-lazy="item.img">
              <img :src="item.img" :alt="item.gname" class="movie-poster">
              <p class="movie-title">{{ item.gname }}</p>
            </div>
          </div>
        </el-card>
      </div>

      <div class="sidebar">
        <el-card class="top-movies">
          <h2 class="section-title top">积分兑换排行</h2>
          <ul class="movie-list">
            <li v-for="(item, index) in topRateMovie" :key="item.gid" class="movie-item" @click="toMovieDetail(item.gid)"
              v-lazy="item.img">
              <span class="movie-rank">{{ index + 1 }}、{{ item.gname }}</span>
              <span class="movie-rating">{{ item.scorerequired }}分</span>
            </li>
          </ul>
        </el-card>
      </div>
    </div>
  </div>
</template>

<style scoped>
.carousel-card {
  width: 100%;
  margin: 0 auto;
  margin-bottom: 24px;
}

.content-container {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.main-content {
    /* 占据剩余空间 */
  flex: 1;
    /* 与侧边栏间距 */
  margin-right: 24px;

}

.sidebar {
    /* 固定25%宽度 */
  flex: 0 0 25%;
}

/* 电影网格布局 */
.movie-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: flex-start;
}

.movie-card {
  width: calc(20% - 16px);
  min-width: 150px;
  border: 1px solid var(--el-border-color);
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
}

.movie-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

/* 图片样式 */
.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.movie-poster {
  width: 100%;
  height: 240px;
  object-fit: cover;
}

/* 文字样式 */
.carousel-title {
  position: absolute;
  bottom: 20px;
  left: 20px;
  font-size: 1.5em;
  color: white;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
}

.section-title {
  padding-bottom: 12px;
  margin-bottom: 16px;
  border-bottom: 2px solid;
}

.hot {
  color: var(--el-color-danger);
  border-bottom-color: var(--el-color-danger-light-3);
}

.classical {
  color: var(--el-color-primary);
  border-bottom-color: var(--el-color-primary-light-3);
}

.top {
  color: var(--el-color-warning);
  border-bottom-color: var(--el-color-warning-light-3);
}

.movie-title {
  padding: 8px;
  text-align: center;
  color: var(--el-text-color-primary);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 榜单样式 */
.movie-list {
  list-style: none;
  padding: 0;
}

.movie-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  border-radius: 4px;
}

.movie-item:hover {
  background-color: var(--el-fill-color-light);
}

.movie-rank {
  font-size: 1.05em;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.movie-rating {
  color: var(--el-color-warning);
  font-weight: bold;
  margin-left: 8px;
}
</style>